﻿using System;
using System.Collections.Generic;

using ExtensibleEventViewer.Console.Adapters;
using ExtensibleEventViewer.Console.DomainModel;


namespace ExtensibleEventViewer.Console.Tasks
{
	internal sealed class RefreshEventLogViewListTask : TaskBase
	{
		public RefreshEventLogViewListTask(IEnumerable<EventLogAdapter> adapters)
		{
			Adapters = adapters;
		}

		public IEnumerable<EventLogAdapter> Adapters
		{
			get; 
			private set;
		}

		protected override void DoExecute()
		{
			if (AppSettings.Instance.EnableAutoRefresh)
			{
				foreach (EventLogAdapter adapter in Adapters)
				{
					if (adapter.Settings.EnableRefresh)
					{
						if (adapter.Settings.UseDefaultRefreshInterval)
						{
							if (DateTime.UtcNow >= adapter.Settings.LastUpdateTime.Add(AppSettings.Instance.RefreshTimeInterval))
							{
								adapter.RefreshRecords();
							}
						}
						else if (DateTime.UtcNow >= adapter.Settings.LastUpdateTime.Add(adapter.Settings.RefreshInterval))
						{
							adapter.RefreshRecords();
						}
					}
				}
			}
		}
	}
}